package com.logic.meeting.app.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import com.logic.meeting.core.app.Atmos;

/**
 * 文 件 名: ApkManager.java
 * 描    述: apk管理类
 * 创 建 人: dingyu
 * 创建时间: 2015-12-10
 */
public class ApkManager {
    /**
     * 获取版本名
     */
    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    /**
     * 获取版本号
     */
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }

    /**
     * 根据包名检查APK是否安装
     */
    public boolean checkApkExist(String packageName) {
        if (null == packageName || "".equals(packageName))
            return false;
        try {
            Atmos.getApplicationContext().getPackageManager().getApplicationInfo(packageName,
                    PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (NameNotFoundException e) {
            return false;
        }
    }

    /**
     * 根据文件的路径检查APK是否安装
     */
    public boolean checkApkIsInstalled(String path) {
        if (null == path || "".equals(path)) {
            return false;
        }
        String apkPath = "file://" + path;
        PackageManager pm = Atmos.getApplicationContext().getPackageManager();
        pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        /*  PackageInfo packageInfo = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        ApplicationInfo appInfo = packageInfo.applicationInfo;
        *//** 得到包名 *//*
        String packageName = packageInfo.packageName;
        *//** apk的绝对路劲 *//*
         *//** apk的版本名称 String *//*
        String versionName = packageInfo.versionName;
        *//** apk的版本号码 int *//*
        int versionCode = packageInfo.versionCode;*/
        return true;
    }
}
